<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <el-table
          :data="roleList"
          style="width: 100%"
          stripe border
      >
        <el-table-column
            type="expand"
        >
          <template v-slot="scope">
            <!--          <pre>{{scope.row}}</pre>-->
            <el-row :class="['bdbtm',index===0 ? 'bdtop' : '','vcenter']" v-for="(item1,index) in scope.row.children"
                    :key="index">
              <!--            设置一级权限-->
              <el-col :span="5">
                <el-tag closable @close="removeUserByID(scope.row,item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!--            设置二级权限-->
              <el-col :span="19">
                <!--              渲染二级权限-->
                <el-row :class="[index2===0? '' : 'bdtop2','vcenter']" v-for="(item2,index2) in item1.children"
                        :key="item2.id">
                  <el-col :span="5">
                    <el-tag type="success" closable @close="removeUserByID(scope.row,item2.id)">{{item2.authName}}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                    <!--                  三级菜单渲染-->
                  </el-col>
                  <el-col :span="14">
                    <el-row v-for="(item3,index3) in item2.children" :key="item3.id">
                      <el-tag type="warning" closable @close="removeUserByID(scope.row,item3.id)">{{item3.authName}}
                      </el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-row>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

          </template>

        </el-table-column>
        <el-table-column
            type="index"
        >
        </el-table-column>
        <el-table-column
            label="角色名称"
            prop="roleName"
        >
        </el-table-column>
        <el-table-column
            label="角色描述"
            prop="roleDesc"
        >
        </el-table-column>
        <el-table-column
            label="操作"
        >
          <template v-slot="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting" @click="setRoleRights(scope.row)">分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
        title="分配用户角色"
        :visible.sync="roleRight"
        width="50%"
        @close="clearArr"
    >
      <el-tree :data="rightList" ref="treeright" :props="rightProps" :default-checked-keys="checkedKeys" node-key="id"
               show-checkbox default-expand-all></el-tree>
      <span slot="footer" class="dialog-footer">
  <el-button @click="roleRight = false">取 消</el-button>
  <el-button type="primary" @click="allotRights">确 定</el-button>
</span>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    name: "roles",
    data() {
      return {
        roleList: [],
        roleRight: false,
        rightList: [],
        rightProps: {
          children: 'children',
          label: 'authName'
        },
        checkedKeys: [],
        roleID: ''
      }
    },
    created() {
      this.getRoleList()
    },
    methods: {
      async getRoleList() {
        const {data: res} = await this.$http.get('/roles')
        if (res.meta.status !== 200) {
          return this.$message.error('请求数据失败')
        }
        this.roleList = res.data
      },
      // 三级权限删除方法
      removeUserByID(role, rightID) {
        this.$confirm('此操作将删除用户权限, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          this.$message({
            type: 'success',
            message: '删除成功!',
          });
          const res = await this.$http.delete(`roles/${role.id}/rights/${rightID}`)
          if (res.status !== 200) return this.$message.error('斯米马赛，删除失败')
          this.$message.success('删除成功')
          role = res.data
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      // 获取角色树弹窗
      async setRoleRights(node) {
        // console.log(9999999);
        // console.log(node.id);
        this.roleID = node.id;
        const {data: res} = await this.$http.get('rights/tree')
        console.log(res);
        if (res.meta.status !== 200) {
          return this.$message.error('更改权限失败')
        }
        this.rightList = res.data
        this.getLeafKeys(node, this.checkedKeys)
        this.roleRight = true
      },
      // 递归获取三级节点的id值
      getLeafKeys(node, arr) {
        // 如果node节点不包含children属性，则是三级节点

        if (!node.children) {
          return arr.push(node.id)
        }
        node.children.forEach(item => {
          this.getLeafKeys(item, arr)
        })
      },
      clearArr() {
        this.checkedKeys = []
      },
      async allotRights() {
        const arrRights = [...this.$refs.treeright.getCheckedKeys(),
          ...this.$refs.treeright.getHalfCheckedKeys()]
        // console.log(arrRights);
        let arrRightsStr = arrRights.join(',')
        const {data: res} = await this.$http.post(`roles/${this.roleID}/rights`, {rids: arrRightsStr})
        // console.log(res);
        if (res.meta.status !== 200) {
          return this.$message.error('更改用户状态失败')
        }
        this.$message.success('用户状态更改成功呢')
        this.getRoleList()
        this.roleRight = false
      },
      setRight() {

      }

    }
  }
</script>
<style scoped>
  .bdtop {
    border-top: 1px solid #eee;
  }

  .bdbtm {
    border-bottom: 1px solid #eee;
  }

  .el-tag {
    margin: 7px;
  }

  .bdtop2 {
    border-top: 1px solid #eee;
  }

  .vcenter {
    display: flex;
    align-items: center;
  }

</style>